/**
 * 
 */
package cn.jhz.filesharingsystem.service;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.jhz.filesharingsystem.dao.PermissionDao;
import cn.jhz.filesharingsystem.model.Pager;
import cn.jhz.filesharingsystem.model.Permission;

/**
 * @author asus
 *
 */
@Service("permissionService")
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

	
	@Override
	public List<Permission> getAllPermission(boolean state) {
		return ((PermissionDao)baseDao).getAllPermission(state);
	}

	@Override
	public Pager<Permission> getAllPagerPermission() {
		return ((PermissionDao)baseDao).getAllPagerPermission();
	}

	/**
	  * 获取initwebServlet初始化权限时,获取到的权限标记list,写入数据库
	 */
	@Override
	public void initPermissions(List<String> resources) {
		//循环遍历list集合,判断标记值,是否存在permission表中
		//不存在放入,存在放弃
		Permission permission;
		boolean isExist = false;
		for (String url : resources) {
			if(url != null) {
				isExist = ((PermissionDao)baseDao).isExistResource(url) ; //true存在,false不存在
				if(!isExist) {
					permission = new Permission();
					permission.setResource(url);
					permission.setState(1);
					((PermissionDao)baseDao).add(permission);
					System.out.println(permission);
				}
			}
		}
	}

	@Override
	public void updateState(Integer permissionId) {
		Permission permission = baseDao.load(permissionId);
		permission.setState(permission.getState()>0?0:1);
		baseDao.update(permission);
		
	}

}
